package com.chenjj.java.thread.waitnotify;

public class PrintABC {
    public static Object lock = new Object();
    public static boolean isThreadA = true;
    public static boolean isThreadB = false;
    public static boolean isThreadC = false;
    public static int Atime = 0;
    public static int Btime = 0;
    public static int Ctime = 0;

    public static void main(String[] args) {
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                    while (Atime < 10){
                        synchronized (lock){
                            if (isThreadA){
                                System.out.println("A");
                                isThreadA = false;
                                isThreadB = true;
                                isThreadC = false;
                                Atime++;
                                lock.notifyAll();
                            }else{
                                try {
                                    lock.wait();
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                    }
            }
        });


        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                while (Btime < 10){
                    synchronized (lock){
                        if (isThreadB){
                            System.out.println("B");
                            isThreadA = false;
                            isThreadB = false;
                            isThreadC = true;
                            Btime++;
                            lock.notifyAll();
                        }else{
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                }
            }
        });

        Thread threadC = new Thread(new Runnable() {
            @Override
            public void run() {
                while (Ctime < 10){
                    synchronized (lock){
                        if (isThreadC){
                            System.out.println("C");
                            isThreadA = true;
                            isThreadB = false;
                            isThreadC = false;
                            Ctime++;
                            lock.notifyAll();
                        }else{
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                }
            }
        });


        threadB.start();
        threadC.start();
        threadA.start();
    }
}
